package hello.crap;
import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;

//A first MIDlet with simple text and a few commands.
public class HelloMessaging extends MIDlet implements ItemStateListener, CommandListener, MessageListener  {

  //The exit commands
  private Command exitCommand; 
  //Command to get Message 
  private Command sendMsgCommand;
  private Command sendMsgEmail;
  TextField phoneNumber ;
  TextField msgText;
   
  //The display for this MIDlet
  private Display display;
  
  Form displayForm;
  //String msgToSend="Can you hear me?";
  
  MessageConnection clientConn;
  
  public HelloMessaging() {
    display = Display.getDisplay(this);
    exitCommand = 
    new Command("Exit", Command.SCREEN, 1);
    sendMsgCommand = new Command("Send", Command.SCREEN, 1);
    sendMsgEmail  = new Command("Send Email", Command.SCREEN, 1);
 }
 
public void notifyIncomingMessage(MessageConnection conn) 
 {
 
 }
 
 // Start the MIDlet by creating the TextBox and 
  // associating the exit command and listener.


  public void startApp() {
     displayForm = new Form("Send Message");
     phoneNumber =  new TextField("Phone:", null, 60, TextField.PHONENUMBER);
     msgText =  new TextField("MSG:", null, 60, TextField.ANY);
     displayForm.append(phoneNumber);
     displayForm.append(msgText);
   displayForm.addCommand(exitCommand);
    displayForm.addCommand(sendMsgCommand);
    displayForm.addCommand(sendMsgEmail);
     displayForm.setCommandListener(this);
   displayForm.setItemStateListener(this);
   display.setCurrent(displayForm);
   
   try
   {
   clientConn = (MessageConnection)Connector.open("sms://+18643630999:5000");
   //clientConn = (MessageConnection)Connector.open("sms://+" + phoneNumber.getString());
   
   
   }
   catch (IOException ioExc)
   {
   System.out.println("Client connection could not be obtained");
   ioExc.printStackTrace();
   } 


  }
  
  // Pause is a no-op because there is no   background
  // activities or record stores to be closed.
  public void pauseApp() { }

  // Destroy must cleanup everything not handled 
  // by the garbage collector.
  public void destroyApp(boolean unconditional) { }

 // Respond to commands. 
  public void commandAction(Command c, Displayable s) {
    if (c == exitCommand) {
        destroyApp(false);
        notifyDestroyed();
    }
  
  if (c == sendMsgCommand) {
    try
    {
      TextMessage tmsg =(TextMessage)clientConn.newMessage(MessageConnection.TEXT_MESSAGE);
          //tmsg.setAddress("sms://18643630999:5000");
          tmsg.setAddress("sms://"+phoneNumber.getString());
          tmsg.setPayloadText(msgText.getString());
          clientConn.send(tmsg);
    }
    catch (Exception exc)
    {
      exc.printStackTrace();
    }
  }
  
  
  }

/* (non-Javadoc)
 * @@see javax.microedition.lcdui.ItemStateListener#itemStateChanged(javax.microedition.lcdui.Item)
 */
public void itemStateChanged(Item arg0) {
    // TODO Auto-generated method stub
    
}
  

 
 }

